package com.wu.cloud.userservice.service.impl;

import bean.PeskResponseEntity;
import bean.UserLoginEntity;
import bean.UserRegisterEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wu.cloud.userservice.dao.UserAccountMapperDao;
import com.wu.cloud.userservice.entity.db.UserTableEntity;
import com.wu.cloud.userservice.entity.vo.UserVo;
import com.wu.cloud.userservice.service.UserAccountService;
import exceptions.SqlExecuteException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.ResponseUtil;
import util.TimeUtil;

import java.util.List;
import java.util.Map;

/**
 * @author xwu36
 */
@Service("userAccountServiceImpl")
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapperDao, UserTableEntity> implements UserAccountService {
    @Autowired
    UserAccountMapperDao userAccountMapperDao;

    @Override
    public PeskResponseEntity login(UserLoginEntity userLoginEntity) {
        PeskResponseEntity responseEntity = null;
        QueryWrapper<UserTableEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UserTableEntity> lambda = queryWrapper.lambda();
        lambda.eq(UserTableEntity::getUserId, userLoginEntity.getUserId());
        lambda.eq(UserTableEntity::getUserKey, userLoginEntity.getPassword());

        List<UserTableEntity> userTableEntities = null;
        try {
            userTableEntities = userAccountMapperDao.selectList(lambda);
        } catch (Exception e) {
            throw new SqlExecuteException(e.getMessage(), e.getCause());
        }
        if (userTableEntities.size() > 0) {
            responseEntity = ResponseUtil.success(userLoginEntity.getUserId() + "登录成功");
        } else {
            responseEntity = ResponseUtil.fail(userLoginEntity.getUserId() + "登录失败");
        }
        return responseEntity;
    }

    @Override
    public List<Map<String, Object>> findUserByUserName(String username) {
        return userAccountMapperDao.findByUsername(username);
    }

    @Override
    public PeskResponseEntity register(UserRegisterEntity userRegisterEntity) {
        UserTableEntity userTableEntity = new UserTableEntity();
        userTableEntity.setUserId(userRegisterEntity.getUserId());
        userTableEntity.setUserName(userRegisterEntity.getUserName());
        userTableEntity.setUserKey(userRegisterEntity.getPassword());
        userTableEntity.setUserAge(userRegisterEntity.getUserAge());
        userTableEntity.setUserSex(userRegisterEntity.getUserSex());
        userTableEntity.setUserDesc(userRegisterEntity.getUserDesc());
        userTableEntity.setReginDate(TimeUtil.getInstance().getNowDefaultPattenTime());

        int update = 0;
        try {
            update = userAccountMapperDao.insert(userTableEntity);
        } catch (Exception e) {
            throw new SqlExecuteException(e.getMessage(), e.getCause());
        }
        PeskResponseEntity responseEntity = null;
        if (update == 1) {
            responseEntity = ResponseUtil.success(userRegisterEntity.getUserId() + "注册成功");
        } else {
            responseEntity = ResponseUtil.fail(userRegisterEntity.getUserId() + "注册失败");
        }
        return responseEntity;
    }

    @Override
    public PeskResponseEntity forget(String uid, String password) {
        return null;
    }

    @Override
    public PeskResponseEntity queryOldPassword(String uid) {
        return null;
    }

    @Override
    public PeskResponseEntity queryUserList(UserVo userVo) {
        QueryWrapper<UserTableEntity> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<UserTableEntity> lambda = queryWrapper.lambda();
        if (userVo.getUid() != null) {
            lambda.eq(UserTableEntity::getUserId, userVo.getUid());
        }
        if (userVo.getuName() != null) {
            lambda.like(UserTableEntity::getUserName, "%" + userVo.getuName() + "%");
        }
        if (userVo.getSex() != -1) {
            lambda.eq(UserTableEntity::getUserSex, userVo.getSex());
        }
        if (userVo.getState() != -1) {
            lambda.eq(UserTableEntity::getUserState, userVo.getState());
        }
        List<UserTableEntity> userTableEntities = userAccountMapperDao.selectList(queryWrapper);
        return ResponseUtil.success(userTableEntities);
    }

}
